//
//  ViewController.m
//  UI12_homework_网易
//
//  Created by aaaaaa on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "JokesModel.h"
#import "TextTableViewCell.h"
#import "TextAndImageTableViewCell.h"
#import "GetHeightTools.h"

#import "CustomView.h"

#define TEXTCELL @"textCell"
#define TEXTANDIMAGECELL @"textAndImageCEll"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray<JokesModel *> *jokesModelArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    
    [self createTableView];
    
//    CustomView *custom = [[CustomView alloc] initWithFrame:CGRectMake(0, 64, 414, 50)];
//    [self.view addSubview:custom];
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"网易";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarAction:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"直播.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction:)];
    
}

- (void)leftBarAction:(UIBarButtonItem *)bar{
    
}

- (void)rightBarAction:(UIBarButtonItem *)bar{
    
}

- (void)handleData{
    
    self.jokesModelArray = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"duanzi" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *array = [dictionary objectForKey:@"段子"];
    
    for (NSDictionary *dic in array) {
        JokesModel *jokesModel = [[JokesModel alloc] init];
        [jokesModel setValuesForKeysWithDictionary:dic];
        [self.jokesModelArray addObject:jokesModel];
    }
}

- (void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    
    
    [tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:TEXTCELL];
    [tableView registerClass:[TextAndImageTableViewCell class] forCellReuseIdentifier:TEXTANDIMAGECELL];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
    [self.view addSubview:tableView];
    [tableView release];
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.jokesModelArray.count;
}

//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JokesModel *jokesModel = self.jokesModelArray[indexPath.row];
    
    if (jokesModel.img == nil) {
        TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TEXTCELL];
        cell.jokesModel = jokesModel;
        
        
        return cell;
    } else {
        TextAndImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TEXTANDIMAGECELL];
        cell.jokesModel = jokesModel;
        return cell;
    }
    
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JokesModel *jokesModel = self.jokesModelArray[indexPath.row];
    
    if (jokesModel.img == nil){
        return [GetHeightTools getTextHeight:jokesModel.digest] + 10 + 50 ;
    } else {
        return [GetHeightTools getTextHeight:jokesModel.digest] + 10 + [GetHeightTools getImageHeight:jokesModel.img] + 50;
    }


}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
